﻿import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
import Qt.labs.platform

import FilUI
import "../page"
import "../controls"

FilWindow {
    id: window
    title: "File Isolation"
    width: 1088
    height: 544
    minimumWidth: 1088 //816
    minimumHeight: 544 //408
    windowIcon                                  : "qrc:/res/image/favicon_nbg.png"
    customAppBar                                : id_com_custom_appbar
    appBar                                      : FilAppBar {
        title                                   : window.title
        height                                  : 35
        showClose                               : window.showClose
        showMinimize                            : window.showMinimize
        showMaximize                            : window.showMaximize
        showStayTop                             : window.showStayTop
        icon                                    : window.windowIcon
        customCom                               : window.customAppBar
        closeClickListener: () => {
            dialog_close.open()
        }
        z: 7
    }

    property alias rootStackView                : id_stackview

    QtObject {
        id: d
        // curent status
        property bool settingsPageClosed            : true
        property bool globalLoginSucceed            : false
        property string sayhelloText                : qsTr("Hi")
        // property boot global
    }

    Component {
        id: id_com_custom_appbar
        RowLayout {
            spacing                             : 0
            height                              : appBar.height
            FilTextButton {
                id: id_filtextbutton__sayhello
                Layout.preferredWidth           : implicitWidth
                Layout.preferredHeight          : parent.height
                text                            : d.sayhelloText
                font.pixelSize                  : 11
                onClicked                       : {
                    id_filmenu__hello.popup()
                }
                FilMenu {
                    id: id_filmenu__hello
                    FilMenuItem {
                        iconSource: FilIcons.Website
                        text: qsTr("Open Website")
                        onTriggered: {
                            console.log("onTriggered text:%1".arg(text))
                            Qt.openUrlExternally("http://192.168.11.88")
                        }
                    }
                    FilMenuSeparator { }
                    FilMenuItem {
                        iconSource: FilIcons.SignOut
                        text: qsTr("Logout")
                        enabled: d.globalLoginSucceed
                        onTriggered: {
                            console.log("onTriggered text:%1".arg(text))
                            d.globalLoginSucceed = false
                            d.sayhelloText = qsTr("Hi")
                            rootStackView.popToIndex(0)
                        }
                    }
                }
            }

            // 设置
            FilIconButton {
                id: btn_settings
                Layout.preferredWidth           : 40
                Layout.preferredHeight          : parent.height
                padding                         : 0
                verticalPadding                 : 0
                horizontalPadding               : 0
                iconSource                      : FilIcons.Settings
                Layout.alignment                : Qt.AlignVCenter
                iconSize                        : 11
                visible                         : true
                text                            : qsTr("Settings")
                radius                          : 0
                iconColor                       : Qt.rgba(7/255, 7/255, 7/255, 1)
                color                           : {
                    if (pressed) {
                        return Qt.rgba(0, 0, 0, 0.06)
                    }
                    return hovered ? Qt.rgba(0, 0, 0, 0.03) : Qt.rgba(0, 0, 0, 0)
                }
                onClicked                       : {
                    id_settingspage.visible = !id_settingspage.visible
                }
            }
        }
    }

    StackView {
        id: id_stackview
        anchors.fill: parent
        initialItem: id_com__loginpage //id_com__mainpage
        visible: !id_settingspage.visible
    }

    // 业务逻辑页
    Component {
        id: id_com__servicepage
        ServicePage {
            id: id_servicepage
            parentStackView: id_stackview
        }
    }

    // 登录页
    Component {
        id: id_com__loginpage
        Item {
            LoginPage {
                id: id_loginpage
                anchors.centerIn: parent
                onLoginSucceed: {
                    console.log("Login Succeed.")
                    d.sayhelloText = "Hi," + SettingsHelper.getUserName()
                    d.globalLoginSucceed = true
                    rootStackView.push(id_com__servicepage)
                }
            }
        }
    }

    // 设置页
    SettingsPage {
        id: id_settingspage
        visible: false
        anchors.fill: parent
        onClickBack: {
            visible = false
        }
        parentStackView: id_stackview
    }

    // 窗口显示帧率
    FpsItem {
        id:fps_item
    }
    Text{
        text: "%1 stack %2 fps %3"
                .arg(AppInfo.debugEnable ? "DEBUG" : "")
                .arg(id_stackview.depth)
                .arg(fps_item.fps)
        opacity: 0.3
        anchors{
            bottom: parent.bottom
            right: parent.right
            bottomMargin: 5
            rightMargin: 5
        }
    }

    SystemTrayIcon {
        id:system_tray
        visible: true
        icon.source: "qrc:/res/image/favicon_nbg.png"
        tooltip: "SecAssistPro"
        menu: Menu {
            MenuItem {
                text: qsTr("Quit")
                onTriggered: {
                    window.close()
                }
            }
        }
        onActivated: (reason)=>{
            if (reason === SystemTrayIcon.Trigger) {
                window.show()
                window.raise()
                window.requestActivate()
            }else if(reason === SystemTrayIcon.Context){
                system_tray.menu.open()
            }
        }
    }

    Timer {
        id: timer_window_hide_delay
        interval: 150
        onTriggered: window.hide()
    }

    FilContentDialog {
        id: dialog_close
        title: qsTr("Quit")
        message: qsTr("Are you sure you want to exit the program ?")
        negativeText: qsTr("Minimize")
        buttonFlags: FilContentDialogType.NegativeButton | FilContentDialogType.NeutralButton | FilContentDialogType.PositiveButton
        onNegativeClicked: {
            system_tray.showMessage(qsTr("Friendly Reminder"), qsTr("SecAssistPro is hidden from the tray, click on the tray to activate the window again."))
            timer_window_hide_delay.restart()
        }
        positiveText: qsTr("Quit")
        neutralText: qsTr("Cancel")
        onPositiveClicked: {
            window.close()
        }
    }

}
